package com.liu.hooks;

import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.protocol.HttpContext;

import com.liu.util.TrackerLoger;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

public class NetworkHook extends AbstractHook {

	public static final String TAG = "#NetWorkHook#";
	
	public static final String TYPE = "com.liu.hooks.NetworkHook";
	
	public XC_MethodHook httpClientHook = new XC_MethodHook() {
		
		@Override
		protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
			
			if(param.args[1] instanceof HttpGet) {
				
				HttpGet get = (HttpGet) param.args[1];
				TrackerLoger.log(TAG, "[httpGetUrl]" + get.getURI().toURL().toString(), TYPE, true);
				
			}
			
			if(param.args[1] instanceof HttpPost) {
				
				HttpPost post = (HttpPost) param.args[1];
				TrackerLoger.log(TAG, "[httpPost]" + post.getURI().toURL().toString(), TYPE, true);
				
				HttpEntity entity = post.getEntity();
				
				InputStream input = entity.getContent();
				
				byte[] buffer = new byte[1024];
				int length = 0;
				StringBuilder sBuilder = new StringBuilder();
				
				while((length = input.read(buffer)) != -1) {
					sBuilder.append(new String(buffer, 0, length, "UTF-8"));
				}
				
//				if(Common.PACKAGENAME == "com.chinatelecom.bestpayclient") {
//					
//					List<NameValuePair> pairs = new ArrayList<NameValuePair>();
//					
//					if(sBuilder.toString().contains("method=createBarCode")) {
//						for(NameValuePair pair : URLEncodedUtils.parse(entity)) {
//							if(pair.getName().equals("ACCOUNTNO")) {
//								pairs.add(new BasicNameValuePair(pair.getName(), "18916019989"));
//							}else {
//								pairs.add(pair);
//							}
//						}
//						
//						URLEncodedUtils.format(pairs, "utf-8");
//						TrackerLoger.log(TAG, "[httpPostPayload-modify]" + URLDecoder.decode(sBuilder.toString(), "utf-8"), TYPE, true);
//						post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
//					}
//				}
				
				TrackerLoger.log(TAG, "[httpPostPayload]" + URLDecoder.decode(sBuilder.toString(), "utf-8"), TYPE, true);
				
				for(Header header : post.getAllHeaders()) {
					TrackerLoger.log(TAG, "[httpPostHeader]" + URLDecoder.decode(header.getName() + ":" + header.getValue(), "utf-8"), TYPE, true);
				}
				
			}
			
		}
		
	};
	
	public XC_MethodHook urlConnectionHook = new XC_MethodHook() {
		@Override
		protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
			
			TrackerLoger.log(TAG, "[URLConnection]" + ((URL)param.thisObject).toString(), TYPE, true);
			
		}
	};
	
	@Override
	public void doHook() {
		// TODO Auto-generated method stub
		XposedHelpers.findAndHookMethod("org.apache.http.impl.client.AbstractHttpClient", classloader, "execute", HttpHost.class, HttpRequest.class, HttpContext.class, httpClientHook);
		
		XposedBridge.hookAllMethods(URL.class, "openConnection", urlConnectionHook);
	}

	@Override
	public void setClassLoader(ClassLoader classloader) {
		this.classloader = classloader;
	}

}
